/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package fr.fonzie;

import java.util.Collection;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;

@Aspect
public class ORMAspect
{
    @Around( "call( * (@javax.persistence.Entity *).persist() )" )
    public Object persist( ProceedingJoinPoint jp )
    {
        Object entity = jp.getTarget();
        Fonzie.persist( entity );
        return entity;
    }

    @Around( "call( * (@javax.persistence.Entity *).remove() )" )
    public Object remove( ProceedingJoinPoint jp )
    {
        Object entity = jp.getTarget();
        Fonzie.remove( entity );
        return entity;
    }

    @Around( "call( * (@javax.persistence.Entity *).refresh() )" )
    public Object refresh( ProceedingJoinPoint jp )
    {
        Object entity = jp.getTarget();
        Fonzie.refresh( entity );
        return entity;
    }

    @Around( "call( * (@javax.persistence.Entity *).detach() )" )
    public Object detach( ProceedingJoinPoint jp )
    {
        Object entity = jp.getTarget();
        Fonzie.detach( entity );
        return entity;
    }

    @Around( "call( * (@javax.persistence.Entity *).isManaged(..) )" )
    @SuppressWarnings( { "unchecked", "rawtypes" } )
    public boolean Managed( ProceedingJoinPoint jp )
    {
        Object entity = jp.getTarget();
        return Fonzie.isManaged( entity );
    }

    @Around( "call( * (@javax.persistence.Entity *).getReference(..) )" )
    @SuppressWarnings( { "unchecked", "rawtypes" } )
    public Object getReference( ProceedingJoinPoint jp )
    {
        Class entityClass = jp.getSignature().getDeclaringType();
        return Fonzie.getReference( entityClass, jp.getArgs()[0] );
    }

    @Around( "call( * (@javax.persistence.Entity *).find(..) )" )
    @SuppressWarnings( { "unchecked", "rawtypes" } )
    public Object find( ProceedingJoinPoint jp )
    {
        Class entityClass = jp.getSignature().getDeclaringType();
        return Fonzie.find( entityClass, jp.getArgs()[0] );
    }

    @Around( "call( * (@javax.persistence.Entity *).findBy*(..) )" )
    @SuppressWarnings( { "unchecked", "rawtypes" } )
    public Object findBy( ProceedingJoinPoint jp )
    {
        Class entityClass = jp.getSignature().getDeclaringType();
        MethodSignature signature = (MethodSignature) jp.getSignature();
        String criteria = signature.getName().substring( 6 );
        if ( isSingleResult( signature ) )
        {
            return Fonzie.findSingle( entityClass, criteria, jp.getArgs() );
        }
        return Fonzie.find( entityClass, criteria, jp.getArgs() );
    }

    private boolean isSingleResult( MethodSignature method )
    {
        return !Collection.class.isAssignableFrom( method.getReturnType() );
    }

}
